package com.taoyibao.hongcanting.ui.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.ApiConstant;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.event.EventFinishActivity;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.OrderEatTypeMode;
import com.taoyibao.hongcanting.model.OrderInfoModel;
import com.taoyibao.hongcanting.model.SanFangBaseModel;
import com.taoyibao.hongcanting.ui.home.activity.MenuActivity;
import com.taoyibao.hongcanting.ui.home.activity.PayActivity;
import com.taoyibao.hongcanting.ui.mine.delegate.WaitPayDelegate;
import com.taoyibao.hongcanting.utils.DataUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * @author TangAnna
 * @description: 待支付
 * @date :${DATA} 14:31
 */
public class WaitPayActivity extends BaseActivityPresenter<WaitPayDelegate> implements View.OnClickListener {
    private String mOrderId;//订单ID
    private String mDeskId;//台位ID

    @Override
    protected Class<WaitPayDelegate> getDelegateClass() {
        return WaitPayDelegate.class;
    }


    /**
     * @param context
     */
    public static void open(Context context, String orderId) {
        Intent intent = new Intent(context, WaitPayActivity.class);
        intent.putExtra(CodeConstant.ORDER_ID, orderId);
        context.startActivity(intent);
    }

    /**
     * 从餐厅地图进入使用
     *
     * @param context
     * @param orderId
     * @param deskId  座位ID
     */
    public static void open(Context context, String orderId, String deskId) {
        Intent intent = new Intent(context, WaitPayActivity.class);
        intent.putExtra(CodeConstant.ORDER_ID, orderId);
        intent.putExtra(CodeConstant.DESK_ID, deskId);
        context.startActivity(intent);
    }


    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        EventBus.getDefault().register(this);
        mOrderId = getIntent().getStringExtra(CodeConstant.ORDER_ID);
        viewDelegate.get(R.id.tv_includShoppingCart_next).setOnClickListener(this);
        viewDelegate.get(R.id.tv_includShoppingCart_continueAdd).setOnClickListener(this);
        if (!TextUtils.isEmpty(getIntent().getStringExtra(CodeConstant.DESK_ID))) {
            mDeskId = getIntent().getStringExtra(CodeConstant.DESK_ID);
            getFoodOrderInfo();
            return;
        }
        getOrderEatType();
    }

    /**
     * 获取订单就餐方式
     */
    private void getOrderEatType() {
        JsonApiWrapper.getApiService().orderEatType(SpUtils.getToken(), mOrderId, CodeConstant.SELECT_ORDERID_EAT_TYPE, null, null)
                .compose(new DefaultTransformer<CompanyBaseModel<List<OrderEatTypeMode>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<OrderEatTypeMode>>>(this, false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<OrderEatTypeMode>> companyBaseModel) {
                        if (companyBaseModel.code == NetCode.HTTP_200 && companyBaseModel.data != null && companyBaseModel.data.size() > 0) {
                            OrderEatTypeMode orderEatTypeMode = companyBaseModel.data.get(0);
                            if (orderEatTypeMode.eatType == CodeConstant.EAT_TYPE_TANGSHI) {//堂食
                                mDeskId = companyBaseModel.data.get(0).deskId;
                                getFoodOrderInfo();
                            } else if (orderEatTypeMode.eatType == CodeConstant.EAT_TYPE_WAIDAI) {//外带
                                getTakeoutOrderInfo();
                            }
                        }else {
                            getTakeoutOrderInfo();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        getTakeoutOrderInfo();
                    }
                });
    }

    /**
     * 获取堂食订单详情
     */
    public void getFoodOrderInfo() {
        JsonApiWrapper.getApiService().getDeskOrderInfo(ApiConstant.MASTER_ID, DataUtils.getKey(), ApiConstant.BRANCH_ID, mDeskId)
                .compose(new DefaultTransformer<SanFangBaseModel<OrderInfoModel>>())
                .subscribe(new ProgressSubscriber<SanFangBaseModel<OrderInfoModel>>(this, true) {
                    @Override
                    public void onNext(SanFangBaseModel<OrderInfoModel> sanFangBaseModel) {
                        if (sanFangBaseModel.Code == NetCode.HTTP_200) {
                            if (sanFangBaseModel.Data != null) {
                                viewDelegate.setData(sanFangBaseModel.Data);
                                viewDelegate.setListData(sanFangBaseModel.Data.order_dishs);
                            } else {
                                viewDelegate.setEmptyView(true);
                            }
                        } else {
                            viewDelegate.setEmptyView(true);
                            super.onNext(sanFangBaseModel);
                        }
                    }
                });
    }

    /**
     * 获取外带订单信息
     */
    public void getTakeoutOrderInfo() {
        JsonApiWrapper.getApiService().getOrderInfo(ApiConstant.MASTER_ID, DataUtils.getKey(), ApiConstant.BRANCH_ID, SpUtils.getMemberId(), mOrderId)
                .compose(new DefaultTransformer<SanFangBaseModel<OrderInfoModel>>())
                .subscribe(new ProgressSubscriber<SanFangBaseModel<OrderInfoModel>>(this, true) {
                    @Override
                    public void onNext(SanFangBaseModel<OrderInfoModel> orderInfoModel) {
                        if (orderInfoModel.Code == NetCode.HTTP_200) {
                            if (orderInfoModel.Data != null) {
                                viewDelegate.setData(orderInfoModel.Data);
                                viewDelegate.setListData(orderInfoModel.Data.order_dishs);
                            } else {
                                viewDelegate.setEmptyView(true);
                            }
                        } else {
                            viewDelegate.setEmptyView(true);
                            super.onNext(orderInfoModel);
                        }
                    }
                });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_includShoppingCart_next://去结账
                PayActivity.open(this, mOrderId, SpUtils.getMemberId());
                break;
            case R.id.tv_includShoppingCart_continueAdd://继续点餐
                MenuActivity.open(this);
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 支付成功关闭页面
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventPayFinish(EventFinishActivity event) {
        if (event.paySuccess) {
            finish();
        }
    }
}
